/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sisgraf.impressao;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import java.io.File;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;

		public class ImprimePdf {

		    public void imprimir(ArrayList lister,int cols,String nome,String total,boolean bol,boolean fichaCliente,ArrayList orcamento,ArrayList pedido) throws FileNotFoundException, DocumentException, IOException  {
		        Document doc = null;
		        OutputStream os = null;
                        Date data = new Date();
                        SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");

		        try {
		            //cria o documento tamanho A4, margens de 2,54cm
		            doc = new Document(PageSize.A4, 10, 10, 10, 10);

		            //cria a stream de saída
                            try
                            {
		            os = new FileOutputStream("Relatorio.pdf");
                            }
                            catch(FileNotFoundException e)
                            {
                                 JOptionPane.showMessageDialog(null,"O arquivo já está sendo usado por outro processo!");
                            }
                            PdfWriter.getInstance(doc,os);
                            doc.open();
                            Paragraph pg = new Paragraph("Relatórios Sisgraf  "+formatador.format(data),new Font(FontFamily.TIMES_ROMAN, 07, Font.NORMAL));
                             pg.setSpacingAfter(10);
                             pg.setAlignment(Element.ALIGN_BASELINE);

                            Paragraph p = new Paragraph("Relatorio de "+nome);
                            p.setAlignment(Element.ALIGN_CENTER);
                            p.setSpacingAfter(17);
                            doc.add(pg);
                            doc.add(p);

                            Font f = new Font(FontFamily.TIMES_ROMAN, 07, Font.NORMAL);
                            PdfPTable table = new PdfPTable(cols);
                            PdfPTable table2 = new PdfPTable(1);
                 int i =0;
                 int c =0;
                 int d=0;
                 for(Object list:lister)
                 {
                     if(i<cols)
                     {
                         Paragraph p1 = new Paragraph(list.toString(),new Font(FontFamily.TIMES_ROMAN, 7, Font.BOLD));
                         PdfPCell header = new PdfPCell(p1);
                         header.setBackgroundColor(BaseColor.YELLOW);
                         header.setBorder(0);
                         table.addCell(header);
                     }
                     else
                         if(c<cols)
                     {
                          Paragraph p3 = new Paragraph(list.toString(),f);
                          PdfPCell header1 = new PdfPCell(p3);
                          header1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                          header1.setBorder(0);
                          table.addCell(header1);
                          c++;
                     }
                         else
                         {
                             Paragraph p4 = new Paragraph(list.toString(),f);
                             PdfPCell header2 = new PdfPCell(p4);
                             header2.setBorder(0);
                             table.addCell(header2);
                             d++;
                         }
                     i++;
                     if(d==cols)
                     {
                         c=0;
                         d=0;
                     }
                 }

                 if(bol)
                 {
                      Paragraph p5 = new Paragraph("Total "+total,new Font(FontFamily.TIMES_ROMAN, 9, Font.BOLD));
                      PdfPCell header3 = new PdfPCell(p5);
                      header3.setBorder(0);
                      table2.addCell(header3);
                      table2.setWidthPercentage(20.0f);
                      table2.setHorizontalAlignment(Element.ALIGN_RIGHT);
                 }
                 Paragraph orc=null;
                 Paragraph ped=null;
                 PdfPTable table3 = new PdfPTable(4);
                 PdfPTable table4 = new PdfPTable(5);

                 if(fichaCliente)
                 {
                     orc = new Paragraph("                                               "
                             + "Orçamento",new Font(FontFamily.TIMES_ROMAN, 9, Font.BOLD));
                     orc.setSpacingBefore(5);
                     int x=0;
                     for(Object list:orcamento)
                     {
                         if(x==0)
                         {
                          Paragraph lisorc = new Paragraph("Total:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table3.addCell(header1);
                         }else
                           if(x==1)
                         {
                          Paragraph lisorc = new Paragraph("Pendentes:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table3.addCell(header1);
                         }else
                           if(x==2)
                         {
                          Paragraph lisorc = new Paragraph("Cancelados:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table3.addCell(header1);
                         }
                           else
                         {
                          Paragraph lisorc = new Paragraph("Aprovados:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table3.addCell(header1);
                         }
                         x++;
                     }

                         ped = new Paragraph("                                               "
                             + "Pedido",new Font(FontFamily.TIMES_ROMAN, 9, Font.BOLD));
                     ped.setSpacingBefore(5);
                     int y=0;
                     for(Object list:pedido)
                     {
                         if(y==0)
                         {
                          Paragraph lisorc = new Paragraph("Total:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table4.addCell(header1);
                         }else
                           if(y==1)
                         {
                          Paragraph lisorc = new Paragraph("Pendentes:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table4.addCell(header1);
                         }else
                           if(y==2)
                         {
                          Paragraph lisorc = new Paragraph("Cancelados:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table4.addCell(header1);
                         }
                           else
                               if(y==3)
                         {
                          Paragraph lisorc = new Paragraph("Entregues:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table4.addCell(header1);
                         }
                           else
                         {
                          Paragraph lisorc = new Paragraph("Cancelados:  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 8, Font.NORMAL));
                          PdfPCell header1 = new PdfPCell(lisorc);
                          header1.setBorder(0);
                          table4.addCell(header1);
                         }


                         y++;
                     }

                 }

                      doc.add(table);
                      doc.add(table2);
//                      orc.setSpacingBefore(10);
//                      doc.add(orc);
                      table3.setSpacingBefore(3);
                      table3.setWidthPercentage(70.0f);
                      doc.add(table3);
//                      ped.setSpacingBefore(10);
//                      doc.add(ped);
                      table4.setSpacingBefore(3);
                      table4.setWidthPercentage(70.0f);
                      doc.add(table4);

		        } finally {
	            if (doc != null) {
		                //fechamento do documento
		                doc.close();
		            }
		            if (os != null) {
		               //fechamento da stream de saída
		               os.close();

                                   File pdf = new File("Relatorio.pdf");
    try {
      Desktop.getDesktop().open(pdf);
    } catch(Exception ex) {
      ex.printStackTrace();
      JOptionPane.showMessageDialog(null, "Erro no Desktop: " + ex);
    }
        }
     }
 }

          public void imprimirOrcamento(ArrayList orcamento,ArrayList itemOrcamento) throws DocumentException, BadElementException, MalformedURLException, IOException
          {

              	        Document doc = null;
		        OutputStream os = null;
                        Date data = new Date();
                        SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");

		        try {
		            //cria o documento tamanho A4, margens de 2,54cm
		            doc = new Document(PageSize.A4, 72, 72, 72, 72);

		            //cria a stream de saída
                            try
                            {
		            os = new FileOutputStream("Orcamento.pdf");
                            }
                            catch(FileNotFoundException e)
                            {
                                 JOptionPane.showMessageDialog(null,"O arquivo já está sendo usado por outro processo!");
                            }
                            PdfWriter.getInstance(doc,os);
                            doc.open();
                            Image img = Image.getInstance("images.jpg");
                            img.setAlignment(Element.PARAGRAPH);
                            doc.add(img);
                            Paragraph pg = new Paragraph("Gráfica Modelo PMA Anhanguera   CNPJ: 00000000000\n"
                                    + "Endereço: Av. dos Andradadas, 1111\n"
                                    + "Bairro: Centro, Belo Horizonte - MG\n"
                                    + "Telefone: (31) 3255-5555"
                                    + "\n",new Font(FontFamily.TIMES_ROMAN, 9, Font.ITALIC));
                             pg.setAlignment(Element.ALIGN_CENTER);
                             doc.add(pg);

 Paragraph pgh = new Paragraph("________________________________________________"
         + "____________________________________________________________________"
         + "_______________________________________");
 pgh.setAlignment(Element.ALIGN_LEFT);
 pgh.setSpacingAfter(15);
 doc.add(pgh);

                             Paragraph p = new Paragraph("Orçamento de Serviços Gráficos",new Font(FontFamily.TIMES_ROMAN, 16, Font.BOLD));
                             p.setAlignment(Element.ALIGN_CENTER);
                             p.setSpacingAfter(20);
                                 doc.add(p);
                            PdfPTable table = new PdfPTable(2);
                            PdfPTable table2 = new PdfPTable(new float[] { 0.7f,0.3f });

                            String string="\n";
                            int i=0;
                             for(Object list:orcamento)
                             {
                                 if(i==0)
                                 {
                                    Paragraph p1 = new Paragraph("Número: "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                                    PdfPCell header = new PdfPCell(p1);
                                    header.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                    header.setBorder(0);
                                    table.addCell(header);
                                    Paragraph p2 = new Paragraph(" ");
                                    PdfPCell h1 = new PdfPCell(p2);
                                    h1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                    h1.setBorder(0);
                                    table.addCell(h1);

                                 }
                                 else if(i==1)
                                 {
                                    Paragraph p3 = new Paragraph("Cliente: "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                                    PdfPCell h2 = new PdfPCell(p3);
                                    h2.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                    h2.setBorder(0);
                                    table.addCell(h2);
                                 }
                                 else if(i==2)
                                 {
                                    Paragraph p4 = new Paragraph("CPF: "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                                    PdfPCell h3 = new PdfPCell(p4);
                                    h3.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                    h3.setBorder(0);
                                    table.addCell(h3);
                                 }
                                 else if(i==3)
                                 {
                                    Paragraph p5 = new Paragraph("Email : "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                                    PdfPCell h4 = new PdfPCell(p5);
                                    h4.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                    h4.setBorder(0);
                                    table.addCell(h4);
                                 }
                                 else if(i==4)
                                 {
                                    Paragraph p6 = new Paragraph("Vendedor: "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                                    PdfPCell h5 = new PdfPCell(p6);
                                    h5.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                    h5.setBorder(0);
                                    table.addCell(h5);
                                 }
                                 else
                                 {
                                    Paragraph p7 = new Paragraph("Data Emissao: "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                                    PdfPCell h6 = new PdfPCell(p7);
                                    h6.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                    h6.setBorder(0);
                                    table.addCell(h6);
                                    Paragraph p8 = new Paragraph(" ");
                                    PdfPCell h7 = new PdfPCell(p8);
                                    h7.setBackgroundColor(BaseColor.LIGHT_GRAY);
                                    h7.setBorder(0);
                                    table.addCell(h7);
                                 }
                                 i++;
                             }
                             int j=0;

                           Paragraph p2 = new Paragraph(string,new Font(FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                           PdfPCell header = new PdfPCell(p2);
                           header.setBackgroundColor(BaseColor.LIGHT_GRAY);
                           header.setBorder(0);
                           table.addCell(header);
                           table.setSpacingAfter(20);
                           table.setWidthPercentage(100.0f);
                           doc.add(table);

                           Paragraph p3 = new Paragraph("                              Descrição do(s) Serviço(s)",new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD));
                           PdfPCell header1 = new PdfPCell(p3);
                           header1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                           table2.addCell(header1);

                           Paragraph p4 = new Paragraph("                  Valor",new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD));
                           PdfPCell header2 = new PdfPCell(p4);
                           header2.setBackgroundColor(BaseColor.LIGHT_GRAY);
                           table2.addCell(header2);

                           String descricao="";
                           float total=0;
                           int w=0;
                           for(Object list:itemOrcamento)
                             {
                               if(w==0)
                               {
                                   descricao+="     "+list.toString()+",  ";
                               }
                               else if(w==1)
                                   descricao+="Prazo Entrega: "+list.toString()+" \n";
                               else if(w==2)
                               {
                                    descricao+="     Quantidade do Serviço: "+list.toString();
                                    Paragraph p5 = new Paragraph(descricao,new Font(FontFamily.TIMES_ROMAN, 10, Font.NORMAL));
                                    PdfPCell header3 = new PdfPCell(p5);
                                    table2.addCell(header3);
                               }
                               else if(w==3)
                               {
                                    Paragraph p6 = new Paragraph("                      R$  "+list.toString(),new Font(FontFamily.TIMES_ROMAN, 10, Font.NORMAL));
                                    PdfPCell header4 = new PdfPCell(p6);
                                    total+=Float.parseFloat(list.toString());
                                    table2.addCell(header4);
                               }
                               w++;
                               if(w==4)
                                   w=0;
                             }

                           table2.setWidthPercentage(100.0f);
                           doc.add(table2);

                            PdfPTable table3 = new PdfPTable(new float[]{0.7f,0.3f });

                             Paragraph pghd = new Paragraph("                                                                                       Total",new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD));
                             PdfPCell hed = new PdfPCell(pghd);
                             hed.setBorder(0);
                             table3.addCell(hed);

                             Paragraph ph = new Paragraph("                           R$  "+total,new Font(FontFamily.TIMES_ROMAN, 10, Font.NORMAL));
                             PdfPCell he = new PdfPCell(ph);
                             he.setBackgroundColor(BaseColor.LIGHT_GRAY);
                             he.setBorder(0);
                             table3.addCell(he);
                             table3.setWidthPercentage(100.0f);
                             doc.add(table3);

		        } finally {
	            if (doc != null) {
		                //fechamento do documento
		                doc.close();
		            }
		            if (os != null) {
		               //fechamento da stream de saída
		               os.close();

                                   File pdf = new File("Orcamento.pdf");
    try {
      Desktop.getDesktop().open(pdf);
    } catch(Exception ex) {
      ex.printStackTrace();
      JOptionPane.showMessageDialog(null, "Erro no Desktop: " + ex);
    }
      }
     }

   }

}